package com.king.tv.mvp.fragment;


import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.king.base.util.LogUtils;
import com.king.base.util.ToastUtils;
import com.king.tv.App;
import com.king.tv.R;
import com.king.tv.mvp.base.BaseFragment;
import com.king.tv.mvp.base.BasePresenter;
import com.king.tv.mvp.base.BaseView;
import com.king.tv.util.DensityUtil;
import com.king.tv.util.SPUtils;
import com.pili.pldroid.player.PLOnCompletionListener;
import com.pili.pldroid.player.PLOnErrorListener;
import com.pili.pldroid.player.PLOnInfoListener;
import com.pili.pldroid.player.PLOnPreparedListener;
import com.pili.pldroid.player.PLOnSeekCompleteListener;
import com.pili.pldroid.player.PLOnVideoFrameListener;
import com.pili.pldroid.player.PLOnVideoSizeChangedListener;
import com.pili.pldroid.player.widget.PLVideoTextureView;
import com.pili.pldroid.player.widget.PLVideoView;

import butterknife.BindView;

import static org.greenrobot.eventbus.EventBus.TAG;

/**
 * @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
 * @since 2017/3/8
 */

public class VideoFragment extends BaseFragment<BaseView, BasePresenter<BaseView>> implements PLOnSeekCompleteListener,PLOnPreparedListener, PLOnCompletionListener, PLOnVideoSizeChangedListener, PLOnErrorListener {


    @BindView(R.id.vtv)
    PLVideoTextureView vtv;

    private int mRotation;

    private String url;

    private boolean isFull;

    public static VideoFragment newInstance(String url,boolean isFull) {

        Bundle args = new Bundle();
        VideoFragment fragment = new VideoFragment();
        fragment.url = url;
        fragment.isFull = isFull;
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getRootViewId() {
        return R.layout.fragment_video;
    }

    @Override
    public void initUI() {
//        vtv.setOnBufferingUpdateListener(this);
        vtv.setOnVideoSizeChangedListener(this);
        vtv.setOnInfoListener(mOnInfoListener);
        vtv.setOnVideoFrameListener(new PLOnVideoFrameListener() {
            @Override
            public void onVideoFrameAvailable(byte[] bytes, int i, int i1, int i2, int i3, long l) {
                Log.i("tag","===========时间戳"+l/1000);
            }
        });
        vtv.setOnPreparedListener(this);
        vtv.setOnErrorListener(this);
        vtv.setOnCompletionListener(this);
        vtv.setOnSeekCompleteListener(this);
        if(!TextUtils.isEmpty(url)){
            vtv.setDisplayAspectRatio(PLVideoView.ASPECT_RATIO_PAVED_PARENT);
            vtv.setVideoPath(url);
            vtv.start();
            int num = SPUtils.getInt(getActivity(),"videonum",0);
            SPUtils.putInt(getActivity(),"videonum",num+1);
        }
    }

    @Override
    public void initData() {

    }

    @Override
    public BasePresenter<BaseView> createPresenter() {
        return new BasePresenter(getApp());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(vtv.isPlaying()){
            vtv.stopPlayback();
        }
    }

    @Override
    public void onCompletion() {
        Log.i("tag prepared","end");
        finish();
    }

    public int getmRotation() {
        Log.i("tag",mRotation+"");
        return mRotation;
    }


    @Override
    public boolean onError(int i) {
        Log.i("tag",i+"");
        return false;
    }


    @Override
    public void onVideoSizeChanged(int i, int i1) {
        Log.i("tag",i+"");
    }

    @Override
    public void onPrepared(int i) {
        Log.i("tag prepared",i+"");
    }

    @Override
    public void onSeekComplete() {
        Log.i("tag prepared","seekcomple");
    }

    private PLOnInfoListener mOnInfoListener = new PLOnInfoListener() {
        @Override
        public void onInfo(int what, int extra) {
            Log.i(TAG, "OnInfo, what = " + what + ", extra = " + extra);
            switch (what) {
                case PLOnInfoListener.MEDIA_INFO_BUFFERING_START:
                    break;
                case PLOnInfoListener.MEDIA_INFO_BUFFERING_END:
                    break;
                case PLOnInfoListener.MEDIA_INFO_VIDEO_RENDERING_START:
//                    Utils.showToastTips(PLVideoTextureActivity.this, "First video render time: " + extra + "ms");
                    break;
                case PLOnInfoListener.MEDIA_INFO_AUDIO_RENDERING_START:
                    Log.i(TAG, "First audio render time: " + extra/1000 + "ms");
                    break;
                case PLOnInfoListener.MEDIA_INFO_VIDEO_FRAME_RENDERING:
                    Log.i(TAG, "video frame rendering, ts = " + extra/1000);
                    if(extra/1000 == 10){
                        if(App.getUser()!=null){
                            Log.i(TAG,"=========isvip"+App.getUser().getIsvip());
                            if (App.getUser().getIsvip().equals("N")){
                                ToastUtils.showToast(getActivity(),"当前不是vip，请开通会员");
                                vtv.stopPlayback();
                                finish();
                            }
                        }
                    }
                    break;
                case PLOnInfoListener.MEDIA_INFO_VIDEO_GOP_TIME:
                    Log.i(TAG, "Gop Time: " + extra);
                    break;
                case PLOnInfoListener.MEDIA_INFO_SWITCHING_SW_DECODE:
                    Log.i(TAG, "Hardware decoding failure, switching software decoding!");
                    break;
                case PLOnInfoListener.MEDIA_INFO_METADATA:
                    Log.i(TAG, "===========metadata"+vtv.getMetadata().toString());
                    break;
                case PLOnInfoListener.MEDIA_INFO_VIDEO_BITRATE:
                case PLOnInfoListener.MEDIA_INFO_VIDEO_FPS:
                    vtv.getCurrentPosition();
                    Log.i(TAG, "Connected !"+vtv.getCurrentPosition());
//                    updateStatInfo();
                    break;
                case PLOnInfoListener.MEDIA_INFO_CONNECTED:
                    Log.i(TAG, "Connected !");
                    break;
                case PLOnInfoListener.MEDIA_INFO_VIDEO_ROTATION_CHANGED:
                    Log.i(TAG, "Rotation changed: " + extra);
                    break;
                default:
                    break;
            }
        }
    };
}
